package o;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import com.huawei.appmarket.service.alarm.control.RepeatingTaskManager;
import com.huawei.appmarket.service.alarm.control.RepeatingTaskManager.d;
import java.util.ArrayList;
import java.util.List;

public class dpw {
    private static final dpw ॱ = new dpw();
    private Handler ˊ = new Handler(ContentManager.getInstant().getContext().getMainLooper());
    private b ˋ = null;
    public RepeatingTaskManager ˎ;
    private final List<Message> ˏ = new ArrayList(8);

    class a implements Runnable {
        private Runnable ˋ;
        final /* synthetic */ dpw ॱ;

        private a(dpw o_dpw, Runnable runnable) {
            this.ॱ = o_dpw;
            this.ˋ = runnable;
        }

        public void run() {
            ˋ();
        }

        private void ˋ() {
            Message obtain = Message.obtain(this.ॱ.ˊ, this.ˋ);
            if (this.ॱ.ˋ() != null) {
                obtain.sendToTarget();
                return;
            }
            synchronized (this.ॱ.ˏ) {
                this.ॱ.ˏ.add(obtain);
            }
        }
    }

    protected final class b implements ServiceConnection {
        final /* synthetic */ dpw ˊ;

        protected b(dpw o_dpw) {
            this.ˊ = o_dpw;
        }

        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            try {
                this.ˊ.ˎ = ((d) iBinder).ˏ();
                this.ˊ.ˎ(this.ˊ.ˎ);
                Logger.ˎ("RepeatingTaskServiceProxy", "Bind to RepeatingTaskService sucessfuly");
            } catch (Throwable e) {
                Logger.ˋ("RepeatingTaskServiceProxy", "onServiceConnected error!", e);
            }
        }

        public void onServiceDisconnected(ComponentName componentName) {
            Logger.ˎ("RepeatingTaskServiceProxy", "ServiceDisconnected:" + componentName);
            this.ˊ.ˎ = null;
            this.ˊ.ˋ = null;
        }
    }

    public static dpw ˏ() {
        return ॱ;
    }

    private RepeatingTaskManager ˋ() {
        if (ॱ.ˎ == null) {
            ॱ.ˎ();
            return null;
        }
        ॱ.ˎ.acquireService();
        return ॱ.ˎ;
    }

    private void ˎ() {
        if (this.ˋ == null) {
            try {
                Intent intent = new Intent(ContentManager.getInstant().getContext(), RepeatingTaskManager.class);
                this.ˋ = new b(this);
                Logger.ˎ("RepeatingTaskServiceProxy", "bind to RepeatingTaskService result:" + ContentManager.getInstant().getContext().bindService(intent, this.ˋ, 1));
            } catch (Throwable e) {
                this.ˋ = null;
                Logger.ˋ("RepeatingTaskServiceProxy", "bind service exception", e);
            }
        }
    }

    public void ˏ(Runnable runnable) {
        new Handler(Looper.getMainLooper()).post(new a(runnable));
    }

    public void ॱ() {
        if (this.ˋ != null) {
            Logger.ˎ("RepeatingTaskServiceProxy", "unBind RepeatingTaskService");
            ContentManager.getInstant().getContext().unbindService(this.ˋ);
            this.ˎ = null;
            this.ˋ = null;
        }
    }

    private void ˎ(RepeatingTaskManager repeatingTaskManager) {
        synchronized (this.ˏ) {
            repeatingTaskManager.acquireService(this.ˏ.size());
            for (Message sendToTarget : this.ˏ) {
                sendToTarget.sendToTarget();
            }
            this.ˏ.clear();
        }
    }
}
